<?php
/**
 * Created by PhpStorm.
 * User: lullabies
 * Date: 2019/5/27
 * Time: 10:33
 */

namespace app\admin\model;


use think\Model;

class Exam extends Model
{
    //定义主键
    protected $pk = 'id';
    //定义表名
    protected $table = 'art_exam';
    //设置隐藏字段
    protected $hidden=['status'];
    //定义与科目表的关联
    public function subjects(){
        return $this->hasMany('subjects','exam_id','id');
    }
    //定义与总分表的关联
    public function totalGrade(){
        return $this->hasMany('TotalGrade','exam_id','id');
    }
    //定义与单科分数标的关联
    public function grade(){
        return $this->hasMany('grade','exam_id','id');
    }
    //获取记录总数
    public static function total($condition=''){
        $data=[
            'status'=>1,
        ];
        if(!empty($condition)){
           $data = array_merge($data,$condition);
        }
        return self::where($data)->count();
    }
    /**
     * 根据考试id 获得报考总人数
     */
    public static function getStudentsById($id){
        $result = self::withCount(['totalGrade' =>function($query){
            $query->where('status',1);
        }])->find($id);
        return $result;
    }
    /**
     * 根据考试id获取详情
     */
    public static function getInfoById($id){
        $result = self::with(['subjects'=>function($query){
            $query->field('id,exam_id,name,price,img,content');
        }])->find($id);
        return $result;
    }
}